package com.yksj.healthtalk.adapter;

import java.util.ArrayList;

import org.universalimageloader.core.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.DoctorAdapter.onClickFriendAttentionListener;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.home.MydoctorListener;
/**
 *      "name": "正在服务",
        "id": 0
        "name": "已预约服务",
        "id": 1
        "name": "已取消预约",
        "id": 2
        "name": "服务历史",
        "id": 3
        "name": "未支付服务",
        "id": 4
        "name": "支付失败服务",
        "id": 5
        "name": "可（待）退款服务",	
        "id": 6
        "name": "我关注的医生",
        "id": 7
 * @author Administrator
 *
 */
public class MyDoctorDetailAdpter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private ViewHolder holder;
	private MydoctorListener listener;
	private ArrayList<CustomerInfoEntity> entities;
	private ImageLoader mImageLoader;
	private onClickFriendAttentionListener attentionListener;
	private int id;// 列表的id
	private Drawable attenDrawable;
	private Drawable notAttenDrawable;

	public MyDoctorDetailAdpter(ArrayList<CustomerInfoEntity> entities,Context context, int id) {
		this.id = id;
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.entities = entities;
		if (context instanceof MydoctorListener) {
			listener = (MydoctorListener) context;
		}
		if (context instanceof onClickFriendAttentionListener) {
			attentionListener = (onClickFriendAttentionListener)context;
		}
		attenDrawable = context.getResources().getDrawable(R.drawable.attention);
		notAttenDrawable = context.getResources().getDrawable(R.drawable.not_attention);
		mImageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return entities.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.my_doctor_item, null);
			holder = new ViewHolder();
			holder.mHeader = (ImageView) convertView.findViewById(R.id.head_image);
			holder.mMessage = (TextView) convertView.findViewById(R.id.item_dele);
			holder.mName = (TextView) convertView.findViewById(R.id.name);
			holder.mV = (ImageView) convertView.findViewById(R.id.v);
			holder.mAtt = (ImageView) convertView.findViewById(R.id.is_friend);
			holder.mTime = (TextView) convertView.findViewById(R.id.money);
			holder.mtitle = (TextView) convertView.findViewById(R.id.title_departments);
			holder.mHospital = (TextView) convertView.findViewById(R.id.hospital);
			holder.mSpecial = (TextView) convertView.findViewById(R.id.special);
			holder.mAdd = (TextView) convertView.findViewById(R.id.add);
			holder.mDescribe = (Button) convertView.findViewById(R.id.describe);
			holder.mPay = (Button)convertView.findViewById(R.id.pay);
			holder.level = (ImageView)convertView.findViewById(R.id.level);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final CustomerInfoEntity mCustomerInfoEntity = entities.get(position);
		holder.mName.setText(mCustomerInfoEntity.getName());
		// 是否是医师沙龙
		if (mCustomerInfoEntity.isShow() || mCustomerInfoEntity.isAudit()) {
			holder.mV.setVisibility(View.VISIBLE);
		} else {
			holder.mV.setVisibility(View.GONE);
		}
		
		//头像
		mImageLoader.displayImage(mCustomerInfoEntity.getSex(),mCustomerInfoEntity.getNormalHeadIcon(),holder.mHeader);

		//服务时间
		holder.mTime.setText("");
		// 职称/科室
		holder.mtitle.setText(mCustomerInfoEntity.getDoctorTitle() + "/"+ mCustomerInfoEntity.getOfficeName1());
		// 医院
		holder.mHospital.setText(mCustomerInfoEntity.getHospital());
		// 专长
		holder.mSpecial.setText(mCustomerInfoEntity.getSpecial());
		//服务历史和我的医生显示工作地点 其他的服务类型
		if (id == 7 || id == 3) {
			if (TextUtils.isEmpty(mCustomerInfoEntity.getDoctorWorkaddress())) {
				holder.mAdd.setText("");
			}else {
				holder.mAdd.setText(mCustomerInfoEntity.getDoctorWorkaddress());
			}
		}else {
			if (TextUtils.isEmpty(mCustomerInfoEntity.getServiceTypeName())) {
				holder.mAdd.setText("");
			}else {
				holder.mAdd.setText(mCustomerInfoEntity.getServiceTypeName());
			}
		}
		// 点击头像进入个人资料
		holder.mAtt.setVisibility(View.GONE);
		holder.mDescribe.setBackgroundResource(R.drawable.login_out_btn_selector);
		holder.mDescribe.setTextColor(context.getResources().getColorStateList(R.color.text_blue));
		holder.mMessage.setVisibility(View.GONE);
		holder.mPay.setVisibility(View.GONE);
		if (id == 7) {
			holder.mAtt.setVisibility(View.VISIBLE);
			holder.mDescribe.setVisibility(View.GONE);
			// 关注状态 0 无关系 1 我关注的 2 黑名单 3是客户 4医生
			if (mCustomerInfoEntity.getIsAttentionFriend() != 0
					&& mCustomerInfoEntity.getIsAttentionFriend() != 2) {
				holder.mAtt.setImageDrawable(notAttenDrawable);
			} else {
				holder.mAtt.setImageDrawable(attenDrawable);
			}
		}
		if (!TextUtils.isEmpty(mCustomerInfoEntity.getServiceStatusCode())) {
			final int code = Integer.valueOf(mCustomerInfoEntity.getServiceStatusCode());
			holder.mDescribe.setVisibility(View.VISIBLE);
			if (id == 0) {//正在服务
				holder.mTime.setVisibility(View.VISIBLE);
				holder.mTime.setText(mCustomerInfoEntity.getServiceTime());
				switch (code) {
				case 185:// 已延时，可停止服务
				case 170:// 医生主动延时服务
				case 155:// 医生拒绝延时
				case 160:// 医生同意延时
				case 70:// 客户发起退号，医生未回应
				case 50:// 已支付，服务中
					holder.mDescribe.setText("查看详情");
					break;
				case 150:// 患者请求延时服务
					holder.mDescribe.setText("延时请求中");
					break;
				case 60:// 客户发起退号，医生未回应
					holder.mDescribe.setText("退订中");
					break;
				// case 50://已支付，服务中
				// holder.mDescribe.setText("取消预约");
				default:
					holder.mDescribe.setVisibility(View.GONE);
					break;
				}
			} else if (id == 1) {//已预约服务
				holder.mTime.setVisibility(View.VISIBLE);
				holder.mTime.setText(mCustomerInfoEntity.getServiceTime());
				if (code == 50) {
					holder.mDescribe.setVisibility(View.VISIBLE);
					holder.mDescribe.setText("查看详情");
				} else {
					holder.mDescribe.setVisibility(View.GONE);
				}

			}else if (id == 2) {//已取消预约
				// SERVICE_TYPE_ID (1为普通服务，2为预约时段服务,3为预约面访服务)
				holder.mTime.setVisibility(View.VISIBLE);
				holder.mTime.setText(mCustomerInfoEntity.getServiceTime());
				if (code == 175 || code == 180) {
					holder.mDescribe.setVisibility(View.VISIBLE	);
					holder.mDescribe.setText("取消原因");
				}else {
					holder.mDescribe.setVisibility(View.GONE);
				}
			}else if(id == 3){//服务历史
				holder.mTime.setVisibility(View.VISIBLE);
				holder.mTime.setText(mCustomerInfoEntity.getServiceEnd());
				holder.mDescribe.setBackgroundColor(Color.TRANSPARENT);
				holder.mDescribe.setTextColor(context.getResources().getColorStateList(R.color.tab_txt_orange));
				holder.mDescribe.setText(mCustomerInfoEntity.getRegisterCount() + "次咨询");
			}else if(id == 4){//未支付服务
				holder.mTime.setVisibility(View.GONE);
				holder.mPay.setVisibility(View.VISIBLE);
				holder.mDescribe.setText("查看详情");
			}else if(id == 6){//可（待）退款服务
				holder.mDescribe.setTextColor(context.getResources().getColorStateList(R.color.text_blue));
				if (code == 222||code == 175 || code == 180) {//申请退款
					holder.mDescribe.setText("申请退款");
				}else if(code == 232){//待退款 
					holder.mDescribe.setText("等待退款 ");
				}else if(code == 242){//退款成功
					holder.mDescribe.setText("退款成功");
				}
			}else if(id == 5){
				holder.mTime.setVisibility(View.VISIBLE);
				holder.mTime.setText(mCustomerInfoEntity.getServiceTime());
				holder.mDescribe.setText("查看详情");
			}else {
				holder.mDescribe.setVisibility(View.GONE);
			}
			holder.mDescribe.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (listener != null) {
						// listener.testClick(index);
						if (code != 0) {
							listener.orderDetail(mCustomerInfoEntity);
						}
					}
				}
			});
			
			holder.mPay.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					listener.onClickPay(mCustomerInfoEntity);
				}
			});
			
			holder.mHeader.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (listener != null) {
						listener.onClickHeaderLisenter(mCustomerInfoEntity);
					}
				}
			});
		} else {
			holder.mDescribe.setVisibility(View.GONE);
		}
		
		// 点击取消和添加关注安妮
		holder.mAtt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (attentionListener != null) {
					attentionListener.onClickFriendAttention(
							mCustomerInfoEntity.getIsAttentionFriend(),
							mCustomerInfoEntity, position);
				}
			}
		});
		holder.level.setImageLevel(mCustomerInfoEntity.getDoctorLevel());
		return convertView;
	}
	

	public class ViewHolder {
		ImageView mHeader;// 条数
		TextView mMessage;// 消息条数
		TextView mName;// 昵称
		ImageView mV;// 是否是医生
//		TextView mType;// 面坊 时段
		ImageView mAtt;// 关注
		TextView mTime;// 服务时间
		TextView mtitle;// 职称
		TextView mHospital;
		TextView mSpecial;
		TextView mAdd;// 地址
		Button mDescribe;// 描述
		Button mPay;
		private ImageView level;//医生等级
	}
}
